/**
 * jQuery for module user
 * 
 * @author	huytbt
 * @date	2012-04-19
 * @version	1.0
 */

;if(window.jQuery) (function($){
	if (!jQuery.h) jQuery.extend({h:{}});
	
	jQuery.h.user = {
		/**
		 * Class FormLogin
		 */
		FormLogin : function(element){
			var _this = this;
			_this.instance = $(element);
			
			_this.initForm = function(){
				_this.instance.submit(function(){
					_this.instance.find(':submit').attr("disabled", "true");
					$.ajax({
						dataType: 'JSON',
						type: _this.instance.attr('method'),
						data: _this.instance.serialize(),
						url: _this.instance.attr('action'),
						success: function(response) {
							_this.instance.find(':submit').removeAttr("disabled");
							if (!response.error)
								window.location = response.data.returnUrl;
						}
					});
					
					return false;
				});
				
				return _this;
			};
		}
	};
})(jQuery);

;if(window.jQuery) (function($){
	$(document).ready(function(){
		$('form.h-user-form-login').each(function(){
			var $login = new jQuery.h.user.FormLogin(this);
			$login.initForm();
		});
	});
})(jQuery);